﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DFYE.Web.Models;
using JULONG.WEB.Library;
using JULONG.WEB.Library.expViewResult;
namespace DFYE.Web.Controllers
{
    public class documentController : Controller
    {
        //IQueryable<document> list;
        
        public documentController()
        {

        }

        //
        // GET: /News/
        public ActionResult Index(int id)
        {
            DataSession<entityDBContainer, documentType> TypeDB = new DataSession<entityDBContainer, documentType>();
            if (id == 0)
            {
                ViewData.Model = TypeDB.newSession<document>().query().Where(d => d.able == true).OrderByDescending(s => s.datetime);
            }else{
                documentType dctype = TypeDB.get(d => d.ID == id);
                ViewBag.channel = dctype;
                ViewData.Model = dctype.document.Where(d=>d.able==true).OrderByDescending(s => s.datetime);

            }
            
            return View();
        }

        public ActionResult Show(int id)
        {
            DataSession<entityDBContainer, document> DB = new DataSession<entityDBContainer, document>();
            var entity = DB.get(d => d.ID == id);
            ViewData.Model = entity;
            return View();
        }

        public ActionResult readAjax(int id) {
            DataSession<entityDBContainer, document> DB = new DataSession<entityDBContainer, document>();
            document dc = DB.get(s => s.ID == id);

            return myJson.success(dc);
        }

        public ActionResult readAjax_new(int type,int top)
        {
            DataSession<entityDBContainer, document> DB = new DataSession<entityDBContainer, document>();
            IEnumerable<document> dcs = DB.query(d=>d.able==true).OrderByDescending(s => s.datetime).Take(top);

           

            if (dcs.Count() == 0) {
                return myJson.error(null);
            }
            //var dc = dcs.ElementAt(0);

            return myJson.success(dcs.Select(d => new {d.ID, d.title, d.content, d.datetime ,type=d.documentType.name}).ToList());
        }

    }
}
